package com.rr.middle;

/**
 * @author RR
 * @date 2024/7/7 13:27
 */

/**
 * https://leetcode.cn/problems/check-if-move-is-legal/?envType=daily-question&envId=2024-07-07
 */
public class CheckMove {
    public boolean checkMove(char[][] board, int rMove, int cMove, char color) {
        return isOk(board, rMove, cMove, color, 1, 1) ||
                isOk(board, rMove, cMove, color, 1, -1) ||
                isOk(board, rMove, cMove, color, -1, 1) ||
                isOk(board, rMove, cMove, color, -1, -1) ||
                isOk(board, rMove, cMove, color, 0, -1) ||
                isOk(board, rMove, cMove, color, 0, 1) ||
                isOk(board, rMove, cMove, color, 1, 0) ||
                isOk(board, rMove, cMove, color, -1, 0);
    }

    private boolean isOk(char[][] board, int rMove, int cMove, char color, int xStep, int yStep) {
        int xLength = board.length;
        int yLength = board[0].length;
        char middleColor = color == 'W' ? 'B' : 'W';
        int middleCount = 0;
        while (true) {
            rMove += xStep;
            cMove += yStep;
            if (rMove < 0 || rMove >= xLength || cMove < 0 || cMove >= yLength) {
                return false;
            }
            if (board[rMove][cMove] == middleColor) {
                middleCount++;
                continue;
            } else if (board[rMove][cMove] == color) {
                break;
            } else {
                return false;
            }
        }
        return middleCount > 0;
    }

    public static void main(String[] args) {
        CheckMove checkMove = new CheckMove();
        checkMove.checkMove(new char[][]{{'.', '.', '.', 'B', '.', '.', '.', '.'}, {'.', '.', '.', 'W', '.', '.', '.', '.'}, {'.', '.', '.', 'W', '.', '.', '.', '.'}, {'.', '.', '.', 'W', '.', '.', '.', '.'}, {'W', 'B', 'B', '.', 'W', 'W', 'W', 'B'}, {'.', '.', '.', 'B', '.', '.', '.', '.'}, {'.', '.', '.', 'B', '.', '.', '.', '.'}, {'.', '.', '.', 'W', '.', '.', '.', '.'}}, 4, 3, 'B');
        System.out.println();
    }
}
